package com.smkj.kaoqin.modules.common.api;


import cn.hutool.core.bean.BeanUtil;
import com.smkj.kaoqin.common.result.VResponse;
import com.smkj.kaoqin.common.utils.BaseContext;
import com.smkj.kaoqin.modules.common.mapper.UserMapper;
import com.smkj.kaoqin.modules.common.pojo.db.User;
import com.smkj.kaoqin.modules.common.pojo.dto.UserLoginDTO;
import com.smkj.kaoqin.modules.common.pojo.vo.UserInfoVO;
import com.smkj.kaoqin.modules.common.service.impl.UserServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yztyear
 * @since 2024-09-11
 */
@Tag(name = "用户管理")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Resource
    private UserServiceImpl userService;
    @Resource
    private UserMapper userMapper;

    @Operation(summary = "登录")
    @PostMapping("/login")
    public VResponse login(@RequestBody UserLoginDTO userLoginDTO){
        return userService.login(userLoginDTO);
    }
    @Operation(summary = "注册")
    @PostMapping("/register")
    public VResponse<String> register(@RequestBody UserLoginDTO userLoginDTO){
        userService.register(userLoginDTO);
        return VResponse.success("注册成功");
    }

    @Operation(summary = "退出登录")
    @PostMapping("/logout")
    public VResponse logout(){

        return VResponse.success("退出成功");
    }

    @Operation(summary = "获取用户信息")
    @PostMapping("/info")
    public VResponse<UserInfoVO> info(){
        log.info("线程中的用户账号:{}", BaseContext.getInfo());
        String account = BaseContext.getInfo();
        User user = userMapper.selectUserInfo(Long.valueOf(account));
        UserInfoVO userInfoVO = BeanUtil.copyProperties(user, UserInfoVO.class);
        return VResponse.success(userInfoVO);
    }

}
